home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / libpng / png.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-12  |  10.7 KB  |  359 lines

  1.  
  2. /* png.c - location for general purpose png functions
  3.  
  4.    libpng 1.0 beta 6 - version 0.96
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    Copyright (c) 1996, 1997 Andreas Dilger
  8.    May 12, 1997
  9.    */
  10.  
  11. #define PNG_INTERNAL
  12. #define PNG_NO_EXTERN
  13. #include "png.h"
  14.  
  15. /* Version information for C files.  This had better match the version
  16.    string defined in png.h */
  17. char png_libpng_ver[] = "0.95";
  18.  
  19. /* Place to hold the signiture string for a PNG file. */
  20. png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  21.  
  22. /* constant strings for known chunk types.  If you need to add a chunk,
  23.    add a string holding the name here.  If you want to make the code
  24.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  25. png_byte FARDATA png_IHDR[5] = { 73,  72,  68,  82, '\0'};
  26. png_byte FARDATA png_IDAT[5] = { 73,  68,  65,  84, '\0'};
  27. png_byte FARDATA png_IEND[5] = { 73,  69,  78,  68, '\0'};
  28. png_byte FARDATA png_PLTE[5] = { 80,  76,  84,  69, '\0'};
  29. png_byte FARDATA png_bKGD[5] = { 98,  75,  71,  68, '\0'};
  30. png_byte FARDATA png_cHRM[5] = { 99,  72,  82,  77, '\0'};
  31. png_byte FARDATA png_gAMA[5] = {103,  65,  77,  65, '\0'};
  32. png_byte FARDATA png_hIST[5] = {104,  73,  83,  84, '\0'};
  33. png_byte FARDATA png_oFFs[5] = {111,  70,  70, 115, '\0'};
  34. png_byte FARDATA png_pCAL[5] = {112,  67,  65,  76, '\0'};
  35. png_byte FARDATA png_pHYs[5] = {112,  72,  89, 115, '\0'};
  36. png_byte FARDATA png_sBIT[5] = {115,  66,  73,  84, '\0'};
  37. png_byte FARDATA png_tEXt[5] = {116,  69,  88, 116, '\0'};
  38. png_byte FARDATA png_tIME[5] = {116,  73,  77,  69, '\0'};
  39. png_byte FARDATA png_tRNS[5] = {116,  82,  78,  83, '\0'};
  40. png_byte FARDATA png_zTXt[5] = {122,  84,  88, 116, '\0'};
  41.  
  42. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  43.  
  44. /* start of interlace block */
  45. int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  46.  
  47. /* offset to next interlace block */
  48. int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  49.  
  50. /* start of interlace block in the y direction */
  51. int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  52.  
  53. /* offset to next interlace block in the y direction */
  54. int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  55.  
  56. /* width of interlace block */
  57. /* this is not currently used - if you need it, uncomment it here and
  58.    in png.h
  59. int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  60. */
  61.  
  62. /* height of interlace block */
  63. /* this is not currently used - if you need it, uncomment it here and
  64.    in png.h
  65. int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  66. */
  67.  
  68. /* mask to determine which pixels are valid in a pass */
  69. int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  70.  
  71. /* mask to determine which pixels to overwrite while displaying */
  72. int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  73.  
  74.  
  75. /* Tells libpng that we have already handled the first "num_bytes" bytes
  76.  * of the PNG file signature.  If the PNG data is embedded into another
  77.  * stream we can set num_bytes = 8 so that libpng will not attempt to read
  78.  * or write any of the magic bytes before it starts on the IHDR.
  79.  */
  80. void
  81. png_set_sig_bytes(png_structp png_ptr, int num_bytes)
  82. {
  83.    png_debug(1, "in png_set_sig_bytes\n");
  84.    if (num_bytes > 8)
  85.       png_error(png_ptr, "Too many bytes for PNG signature.");
  86.  
  87.    png_ptr->sig_bytes = num_bytes < 0 ? 0 : num_bytes;
  88. }
  89.  
  90. /* Checks whether the supplied bytes match the PNG signature.  We allow
  91.  * checking less than the full 8-byte signature so that those apps that
  92.  * already read the first few bytes of a file to determine the file type
  93.  * can simply check the remaining bytes for extra assurance.  Returns
  94.  * an integer less than, equal to, or greater than zero if sig is found,
  95.  * respectively, to be less than, to match, or be greater than the correct
  96.  * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  97.  */
  98. int
  99. png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  100. {
  101.    if (num_to_check > 8)
  102.       num_to_check = 8;
  103.    else if (num_to_check < 1)
  104.       return 0;
  105.  
  106.    if (start > 7)
  107.       return 0;
  108.  
  109.    if (start + num_to_check > 8)
  110.       num_to_check = 8 - start;
  111.  
  112.    return (png_memcmp(&sig[start], &png_sig[start], num_to_check));
  113. }
  114.  
  115. /* (Obsolete) function to check signature bytes.  It does not allow one
  116.    to check a partial signature.  This function will be removed in the
  117.    future - use png_sig_cmp(). */
  118. int
  119. png_check_sig(png_bytep sig, int num)
  120. {
  121.   return !png_sig_cmp(sig, (png_size_t)0, (png_size_t)num);
  122. }
  123.  
  124. /* Function to allocate memory for zlib. */
  125. voidpf
  126. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  127. {
  128.    png_voidp ptr;
  129.    png_uint_32 num_bytes;
  130.  
  131.    num_bytes = (png_uint_32)items * size;
  132.    ptr = png_malloc((png_structp)png_ptr, num_bytes);
  133.    if (num_bytes > (png_uint_32)0x8000)
  134.    {
  135.       png_memset(ptr, 0, (png_size_t)0x8000L);
  136.       png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  137.          (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  138.    }
  139.    else
  140.    {
  141.       png_memset(ptr, 0, (png_size_t)num_bytes);
  142.    }
  143.    return (voidpf)(ptr);
  144. }
  145.  
  146. /* function to free memory for zlib */
  147. void
  148. png_zfree(voidpf png_ptr, voidpf ptr)
  149. {
  150.    png_free((png_structp)png_ptr, (png_voidp)ptr);
  151. }
  152.  
  153. /* Reset the CRC variable to 32 bits of 1's.  Care must be taken
  154.    in case CRC is > 32 bits to leave the top bits 0. */
  155. void
  156. png_reset_crc(png_structp png_ptr)
  157. {
  158.    /* set CRC to all 1's */
  159. #ifdef PNG_USE_OWN_CRC
  160.    png_ptr->crc = 0xffffffffL;
  161. #else
  162.    png_ptr->crc = crc32(0, Z_NULL, 0);
  163. #endif
  164. }
  165.  
  166. #ifdef PNG_USE_OWN_CRC
  167. /* Table of CRCs of all 8-bit messages.  By default, we use the tables made
  168.    by zlib, to save some memory.  If you wish to png_malloc() this
  169.    table, turn this into a pointer, and png_malloc() it in make_crc_table().
  170.    You may then want to hook it into png_struct and free it with the
  171.    destroy functions.  Another alternative is to pre-fill the table.  */
  172. static png_uint_32 crc_table[256];
  173.  
  174. /* Flag: has the table been computed? Initially false. */
  175. static int crc_table_computed = 0;
  176.  
  177. /* make the table for a fast crc */
  178. static void
  179. make_crc_table(void)
  180. {
  181.   png_uint_32 c;
  182.   int n, k;
  183.  
  184.   for (n = 0; n < 256; n++)
  185.   {
  186.    c = (png_uint_32)n;
  187.    for (k = 0; k < 8; k++)
  188.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  189.    crc_table[n] = c;
  190.   }
  191.   crc_table_computed = 1;
  192. }
  193.  
  194. /* Update a running CRC with the bytes buf[0..len-1] - the CRC should be
  195.    initialized to all 1's, and the transmitted value is the 1's complement
  196.    of the final running CRC. */
  197. static png_uint_32
  198. update_crc(png_uint_32 crc, png_bytep buf, png_size_t len)
  199. {
  200.   png_uint_32 c;
  201.   png_bytep p;
  202.   png_uint_32 n;
  203.  
  204.   c = crc;
  205.   p = buf;
  206.   n = len;
  207.  
  208.   if (!crc_table_computed)
  209.   {
  210.    make_crc_table();
  211.   }
  212.  
  213.   if (n > 0) do
  214.   {
  215.    c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  216.   } while (--n);
  217.  
  218.   return c;
  219. }
  220. #endif /* PNG_USE_OWN_CRC */
  221.  
  222. /* Calculate the CRC over a section of data.  We can only pass as
  223.    much data to this routine as the largest single buffer size.  We
  224.    also check that this data will actually be used before going to the
  225.    trouble of calculating it. */
  226. void
  227. png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  228. {
  229.    int need_crc = 1;
  230.  
  231.    if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  232.    {
  233.       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  234.           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  235.          need_crc = 0;
  236.    }
  237.    else                                                    /* critical */
  238.    {
  239.       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  240.          need_crc = 0;
  241.    }
  242.  
  243.    if (need_crc)
  244. #ifdef PNG_USE_OWN_CRC
  245.       png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  246. #else
  247.       png_ptr->crc = crc32(png_ptr->crc, ptr, length);
  248. #endif
  249. }
  250.  
  251. /* Allocate the memory for an info_struct for the application.  We don't
  252.    really need the png_ptr, but it could potentially be useful in the
  253.    future.  This should be used in favour of malloc(sizeof(png_info))
  254.    and png_info_init() so that applications that want to use a shared
  255.    libpng don't have to be recompiled if png_info changes size. */
  256. png_infop
  257. png_create_info_struct(png_structp png_ptr)
  258. {
  259.    png_infop info_ptr;
  260.  
  261.    png_debug(1, "in png_create_info_struct\n");
  262.    if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL)
  263.    {
  264.       png_info_init(info_ptr);
  265.    }
  266.  
  267.    return info_ptr;
  268. }
  269.  
  270. /* This function frees the memory associated with a single info struct.
  271.    Normally, one would use either png_destroy_read_struct() or
  272.    png_destroy_write_struct() to free an info struct, but this may be
  273.    useful for some applications. */
  274. void
  275. png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  276. {
  277.    png_infop info_ptr = NULL;
  278.  
  279.    png_debug(1, "in png_destroy_info_struct\n");
  280.    if (info_ptr_ptr != NULL)
  281.       info_ptr = *info_ptr_ptr;
  282.  
  283.    if (info_ptr != NULL)
  284.    {
  285.       png_info_destroy(png_ptr, info_ptr);
  286.  
  287.       png_destroy_struct((png_voidp)info_ptr);
  288.       *info_ptr_ptr = (png_infop)NULL;
  289.    }
  290. }
  291.  
  292. /* Initialize the info structure.  This is now an internal function (0.89)
  293.    and applications using it are urged to use png_create_info_struct()
  294.    instead. */
  295. void
  296. png_info_init(png_infop info_ptr)
  297. {
  298.    png_debug(1, "in png_info_init\n");
  299.    /* set everything to 0 */
  300.    png_memset(info_ptr, 0, sizeof (png_info));
  301. }
  302.  
  303. /* This is an internal routine to free any memory that the info struct is
  304.  * pointing to before re-using it or freeing the struct itself.  Recall
  305.  * that png_free() checks for NULL pointers for us.
  306.  */
  307. void
  308. png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  309. {
  310. #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
  311.    int i;
  312.  
  313.    png_debug(1, "in png_info_destroy\n");
  314.    if (info_ptr->text != NULL)
  315.    {
  316.       for (i = 0; i < info_ptr->num_text; i++)
  317.       {
  318.          png_free(png_ptr, info_ptr->text[i].key);
  319.       }
  320.       png_free(png_ptr, info_ptr->text);
  321.    }
  322. #endif
  323. #if defined(PNG_READ_pCAL_SUPPORTED)
  324.    png_free(png_ptr, info_ptr->pcal_purpose);
  325.    png_free(png_ptr, info_ptr->pcal_units);
  326.    if (info_ptr->pcal_params != NULL)
  327.    {
  328.       for (i = 0; i < info_ptr->pcal_nparams; i++)
  329.       {
  330.          png_free(png_ptr, info_ptr->pcal_params[i]);
  331.       }
  332.       png_free(png_ptr, info_ptr->pcal_params);
  333.    }
  334. #endif
  335.  
  336.    png_info_init(info_ptr);
  337. }
  338.  
  339. /* This function returns a pointer to the io_ptr associated with the user
  340.    functions.  The application should free any memory associated with this
  341.    pointer before png_write_destroy() or png_read_destroy() are called. */
  342. png_voidp
  343. png_get_io_ptr(png_structp png_ptr)
  344. {
  345.    return png_ptr->io_ptr;
  346. }
  347.  
  348. #if !defined(PNG_NO_STDIO)
  349. /* Initialize the default input/output functions for the PNG file.  If you
  350.    use your own read or write routines, you can call either png_set_read_fn()
  351.    or png_set_write_fn() instead of png_init_io(). */
  352. void
  353. png_init_io(png_structp png_ptr, FILE *fp)
  354. {
  355.    png_debug(1, "in png_init_io\n");
  356.    png_ptr->io_ptr = (png_voidp)fp;
  357. }
  358. #endif
  359.